package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @GetMapping("/detail")
    public Object detail(String id) {
        return JsonResult.success(strategyService.get(id));
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }

    @PostMapping("/addComment")
    @RequireLogin
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    @PostMapping("/commentThumb")
    @RequireLogin
    public Object commentThumb(String cid, String sid, @UserParam UserInfo user){
        strategyCommentService.commentThumb(cid,sid,user);
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyCommentService.statisVo(sid, 1));
    }

    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid, @UserParam UserInfo user){
        boolean ret = strategyCommentService.favor(sid,user.getId());
        return JsonResult.success(ret);
    }

    @PostMapping("/strategyThumbup")
    @RequireLogin
    public Object strategyThumbup(String sid, @UserParam UserInfo user){
        boolean ret = strategyCommentService.strategyThumbup(sid,user.getId());
        return JsonResult.success(ret);
    }
}
